package com.zhuoshi.service.impl;

import java.util.List;

import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.zhuoshi.entity.Bargain;
import com.zhuoshi.entity.Client;
import com.zhuoshi.entity.Indent;
import com.zhuoshi.entity.Paymenttype;
import com.zhuoshi.entity.Room;
import com.zhuoshi.mapper.BargainMapper;
import com.zhuoshi.mapper.ClientMapper;
import com.zhuoshi.mapper.IndentMapper;
import com.zhuoshi.mapper.PaymenttypeMapper;
import com.zhuoshi.mapper.RoomMapper;
import com.zhuoshi.service.BargainService;
@Service
@Transactional
public class BargainServiceImpl implements BargainService{
	@Autowired
	private BargainMapper bargainMapper;
	
	@Autowired
	private IndentMapper indentMapper;
	
	@Autowired
	private PaymenttypeMapper paymenttypeMapper;
	
	@Autowired
	private RoomMapper roomMapper;
	
	@Autowired
	private ClientMapper clientMapper;
	
	@Autowired
	private Sid sid;
	
	@Override
	public void addBargain(Model model, Integer id) {
		Indent indent = indentMapper.selectByPrimaryKey(id);
		Room room = roomMapper.selectByPrimaryKey(indent.getRoomid());
		indent.setRoomtotal(room.getRoomtotal());
		indent.setRoomprice(room.getRoomprice());
		indent.setNumber(room.getNumber());
		indent.setArea(room.getArea());
		model.addAttribute("indent", indent);
		model.addAttribute("sid", sid.nextShort());
	}

	@Override
	public void addBargain(Bargain bargain) {
		bargainMapper.insertSelective(bargain);
	}

	@Override
	public List<Bargain> showBargain() {
		List<Bargain> lists = bargainMapper.selectByExample(null);
		for (Bargain bargain : lists) {
			Room room = roomMapper.selectByPrimaryKey(bargain.getRoomid());
			bargain.setNumber(room.getNumber());
			bargain.setRoomtotal(room.getRoomtotal());
			bargain.setRoomprice(room.getRoomprice());
			Client client = clientMapper.selectByPrimaryKey(bargain.getClientid());
			bargain.setClientidname(client.getClientidname());	
			Paymenttype paymenttype = paymenttypeMapper.selectByPrimaryKey(bargain.getPaymenttypeid());
			bargain.setPaymentname(paymenttype.getName());
		}
		return lists;
	}
	
}
